package com.college.common.utils.security;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson2.JSONObject;
import com.college.common.utils.log.LogPrintUtils;
import lombok.extern.slf4j.Slf4j;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * 接口加解密流程
 */
@Slf4j
public class RequestUtil51GJJ {
	//51公钥，生产环境公钥
	private final static String PUCLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxLWGmWQlQ6JIdgTRCqUqxOAb5Yu3ra1jXcdh7uFu5RpgMvJRj80VfYohifUmcyCNjw6K90LZ90wR32oeTr8Y4Z1eg0KvkArIhIwSEHQZ1TZ1Uocg143rRypwU0jbMSLh6poczZ8JepN1VJzpSp7vq4Z6ZcGHjBxpcUYdH8ljSsGhCibMippClQsw/Jrvszq8vFZTWv4qkfGFip1ct1R0lynSFIKqrphvmj7237+n3WjNz9PcFY9k12G6RZTOI70ep7wUtqFZNEWHeWi+raq0cpV+XKQYH7MTLPPXxMmHPV5BS+yqnzruJT06j5DiPaeQ4IInkdjwqJBu/mHbbMAZ3wIDAQAB";

	//己方公钥，提供给51用
	private final static String OUR_PUCLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA04lkdvkugpjrORdSS/ErMmX0WL07dZUI4A5zti92MNPsOIs7M2OPbR9g3hXs244gQFRV9JG+X5rb0bgpGSYcpLaSvGzTUG8+tCbKmFXuB1HOlg93U6aqESF9AgbhnTwzmN8e6OoBEbgspsjGRI+1/ikFTeaxosawSDR7pxOfQ4pzIc+AGMC7F8KckSlovkI3Xl4tsD0DEp8X/3gEYnSP4U16tQO3C8mKnxRmn3wL8DlyiYiDuTQV2Li4esKOAp7GJtxTXPofFaZXb2fjGU3zhPYTVVP+cIb0QVhmdfwRfnx96bH48zjwWlQePZzonMracFc07NOCuDSbGGzDEZsdBwIDAQAB";

	//私钥
	private final static String PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0B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lAoN4ObKn9ZmNztepgpvIwiCaVwO0q9NfmcxdICFuocNh3KeCvx5+yT1bnbjZ8vSb0dkcAq5qs2gL0Y39wBK8+aEq8KfspynJ1cvIY7eLPbCw3johNbsvdGEwWW3hB78uKkuvYKSaGIapheV2avbY0smkRgPNWcKSMCQ/37o9AoGBANl7/twRmVEEIzJ8F4DfmN7sCFDXKtn0pAONhks/tbGl4J9f3zYLtzFnC8MRyKblmQjuIiN8WiapaPBDB6iwNFA2XHQArkSpG25DlPk/nZcIZ3+x/n6YDL0L3mrLKOCYw2GU4ckt7IArTeXI/lPgBCfOl+4e51hSqOzaOqV7zRHVAoGBAOtSpUcW/yANHyKTzBXpS/LNPqSTj4MfhA73hatWXq0bgb2zqC44GhGVjQvC8k3NEgLhpnsQFYDwBx7ezWhni9zZZLxJVn6WYOZ7QdOU7vdPsrOMIcBmprZCANovtIsgwdMP9wv/2bOMCYf0gpzNg4/FePLM7s/9F3LdL6FvGKZj";


	public static void main(String[] args) {
		System.out.println(DigestUtil.md5Hex("fadsf"));
	}

	/**
	 * 组装http请求的入参
	 *
	 * @return
	 */
	public static JSONObject encodeParam(JSONObject paramJson) {
		// http请求入参组装
		JSONObject data = new JSONObject();
		// 业务参数json字符串组装
		String source = paramJson.toString();
		try {
			// 加密部分demo
			// 从字符串中得到公钥
			PublicKey publicKey = RSAUtils.loadPublicKey(PUCLIC_KEY);
			LogPrintUtils.info(log, "需加密的明文：" + source);
			byte[] encryptByte = RSAUtils.encryptData(source.getBytes("utf-8"), publicKey);
			// 为了方便观察把加密后的数据用base64加密转一下，要不然看起来是乱码,所以解密是也是要用Base64先转换
			String afterencrypt = Base64Util.encode(encryptByte);
			LogPrintUtils.info(log, "加密后的密文：" + afterencrypt);// java.net.URLEncoder.encode(afterencrypt,"utf-8"));
			data.put("content", afterencrypt);
			// 从字符串中得到私钥
			PrivateKey privateKey = RSAUtils.loadPrivateKey(PRIVATE_KEY);
			// 加签
			String signStr = RSAUtils.rsaSign(encryptByte, privateKey);
			LogPrintUtils.info(log, "加签后的签名：" + signStr);
			data.put("sign", signStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		LogPrintUtils.info(log, "加密后的json：" + data);
		return data;
	}

	/**
	 * 解密、验签，获取接口业务入参
	 *
	 * @return
	 */
	public static JSONObject decodeParam(String afterencrypt, String signStr) {
		LogPrintUtils.info(log, "需解密的密文：" + afterencrypt);
		JSONObject paramJson = new JSONObject();
		try {
			// 从字符串中得到公钥
			PublicKey publicKey = RSAUtils.loadPublicKey(PUCLIC_KEY);
			// 从字符串中得到私钥
			PrivateKey privateKey = RSAUtils.loadPrivateKey(PRIVATE_KEY);
			// 因为RSA加密后的内容经Base64再加密转换了一下，所以先Base64解密回来再给RSA解密
			byte[] decryptByte = RSAUtils.decryptData(Base64Util.decode(afterencrypt), privateKey);
			String decryptStr = new String(decryptByte);
			LogPrintUtils.info(log, "解密后的明文：" + decryptStr);
			paramJson = JSONObject.parseObject(decryptStr);
			// 验签
			boolean result = RSAUtils.doCheck(Base64Util.decode(afterencrypt), Base64Util.decode(signStr), publicKey);
			LogPrintUtils.info(log, "验签的结果：" + result);
			if (!result) {
				LogPrintUtils.info(log, "验签失败");
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramJson;
	}


	public static JSONObject getResponse(JSONObject param) {
		return encodeParam(param);
	}

}
